<template>
<div>
    <rx-search
        v-model="curValues"
        @search="searchApp"
        :mode="'tags'"
        :single="true"
        :placeholder="$ti18('rxMsa.masWarn.selectApp', '请输入或选择应用')"
        :showIcon="true"
        :showButton="false"
        @change="appChange"
    >
        <my-icon slot="icon" type="iconcaidan"></my-icon>
        <span slot="loading" v-if="loading"><a-spin /></span>
        <rx-search-item v-for="item of appList"
                        :key="item['appId']"
                        :value="item['appId']"
        >
            {{$i18(item['clientName'],'应用名称')}}
        </rx-search-item>
    </rx-search>
</div>
</template>
<script>

import SysAppApi from "@/api/system/core/sysApp";

export default {
    name: "rx-search-app",
    props:{
        value:{//选中的数据;
            type: Array
        }

    },
    data(){
        return {
            curValues:[],
            appList:[],
            loading:false

        }
    },
    created() {
        this.curValues=this.value;
        this.getAllApps("");
    },

    methods:{
        searchApp(val){
            this.appList = [];
            this.loading = true ;
            this.getAllApps(val);
        },
        getAllApps(val){
            var self = this;
            SysAppApi.getAllUserAppsByAppName(val).then(data => {
                self.appList = data;
                self.loading = false;
            })
        },
        appChange(val){
            //如果没选择应用，重新显示所有应用
            if(val.length == 0){
                this.getAllApps("");
                this.$emit("input",[])
                this.$emit("change",[]);
            }else{
               this.curValues=[{label:val[0].label,value:val[0].value}]
               this.$emit("input",this.curValues);
               this.$emit("change",this.curValues);
            }
        },

    },
    watch:{
        value:{
            handler(val){
                this.curValues=val;
            },
            deep:true
        }
    }

}
</script>

<style scoped>
ul,li{
    padding: 0;
    margin: 0;
    list-style: none;
}
.rx-div-edit-close .rx-close-icon {
    display: none;
}
.rx-close-icon:hover{
    color: red;
    cursor: pointer;
}
.rx-div-edit-close{
    z-index: 1;
    position: absolute;
    top: 0;
    right: 0;
    line-height: normal;
    padding: 0;
    width: 0;
    height: 0;
    border-top: 24px solid #d9dce1;
    border-left: 24px solid transparent;
    border-right: 0;
    border-bottom: 0;
    display: none;
}
.rx-div-edit-close:before{
    content: 'x';
    display: inline-block;
    font-size: 12px;
    line-height: 8px;
    height: 10px;
    position: absolute;
    top: -22px;
    right: 4px;
}
.rx-div-edit-close:hover{
    color: #fff;
    border-top-color: #ff4d4f;
}

.text-width{
    position: absolute;
    top: 0;
    opacity: 0;
}
.rx-tags-ul{
    font-size: 0;
    padding-bottom: 3px;

}
.rx-tags-ul-close{
    padding-right: 10px;
}
.rx-tags-ul > .rx-tags-li{
    min-height: 24px;
    line-height: normal;
    font-size: 14px;
    display: inline-block;
    position: relative;
    margin: 3px 3px 0px 0;
    padding: 0 20px 0 4px;
    border-radius: 4px;
    background-color: #eeeeee;
    opacity: 1;
    vertical-align: middle;
    white-space: nowrap;
}
.rx-tags-ul > .rx-tags-li .rx-tags-icon{
    display: inline-block;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background-color: #6683ef;
    color: #fff;
    line-height: 18px;
    text-align: center;
    font-size: 12px;
    vertical-align: -2px;
}
.rx-tags-li  .rx-tags-text{
    display: inline-block;
    vertical-align:middle;
    white-space: normal;
    word-break: break-all;
    max-width: calc( 100% - 16px );
}
.rx-tags-li .rx-tags-icon + .rx-tags-text{
    padding-left: 4px;
}

.rx-delete-li{
    opacity: 0!important;
    transition:opacity 0.2s;
}
.rx-tags-close{
    position: absolute;
    top: 50%;
    margin-top: -6px;
    right: 4px;
    width: 12px;
    height: 12px;
    text-align: center;
    display: inline-block;
    cursor: pointer;
    font-style:normal;
    color:#46494d;
    border-radius: 50%;
    font-size: 11px;
    line-height: 11px;
}
.rx-tags-close:hover{
    color:#FA6854;
}
.rx-tags-ul > .rx-input-li{
    display: inline-block;
    border: 0;
    padding: 0;
    background-color: transparent;
    vertical-align: middle;
}
.rx-edit-input-box{
    position: absolute;
    left: 2px;
    top: 2px;
    bottom: 2px;
    right: 2px;
}
.rx-div-edit-text{
    position: absolute;
    top: 1px;
    bottom: 1px;
    left: 2px;
    right: 2px;
    line-height: 28px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    z-index: 1;
}
.rx-search-loading{
    padding-left: 6px;
    padding-top: 6px;
}
.rx-div-input{
    outline: none;
    border: 0;
    width: 100%;
    padding: 0;
    height: 100%;
    background-color: transparent;
}
.rx-div-input-tags{
    margin-top: 3px;
}
.rx-search-container{
    display: flex;
}

.rx-div-edit-container{
    position: relative;
    border: 1px solid #c7c7c7;
    border-radius: 4px;
    display: flex;
    flex: 1;
    align-self: center;
    z-index: 1;
}
.rx-div-edit-container:not(.rx-div-edit-readonly):hover .rx-div-edit-close{
    display: block;
}
.rx-search-container-button .rx-edit-button{
    border: 1px solid #c7c7c7;
    border-radius: 0 4px 4px 0;
    z-index: 1;
    position: relative;
    background-color: #fff;
    padding:0 6px ;
}

.rx-search-container-button .rx-edit-button:hover{
    border-color: #40a9ff;
    color: #40a9ff;
    cursor: pointer;
}
.rx-search-container-button .rx-div-edit-container{
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
    border-right: 0;
}
.rx-search-container-button .rx-btn-icon-box{
    display: inline-block;
    vertical-align: middle;
}
.rx-search-container-button .rx-edit-button:before{
    content: '';
    height: 100%;
    width: 0;
    display: inline-block;
    vertical-align: middle;
}

.rx-div-search-box{
    padding: 4px 0;
}
.rx-div-edit{
    position: relative;
    flex: 1;
    min-height: 30px;
    padding:2px;
    overflow: hidden;
    box-sizing: border-box;
}
.rx-div-edit-tags{
    padding: 0 0 0 3px;
}


.rx-search-container:hover .rx-edit-button,
.rx-search-container-focus .rx-edit-button{
    border-left-color:#40a9ff;
}
.rx-search-container:not(.rx-div-edit-readonly):hover .rx-div-edit-container,
.rx-search-container-focus .rx-div-edit-container{
    border-color: #40a9ff;
    outline:0;
    cursor: pointer;
}

.rx-div-edit:hover,
.rx-div-edit:focus,
.rx-div-edit:active
{
    border-color: #40a9ff;
    outline:0;
}
/*禁用样式*/
.rx-search-container.rx-div-edit-disabled:hover .rx-edit-button,
.rx-search-container.rx-div-edit-disabled:hover .rx-div-edit-container,
.rx-search-container.rx-div-edit-disabled .rx-div-edit:hover,
.rx-search-container.rx-div-edit-disabled .rx-div-edit:active,
.rx-search-container.rx-div-edit-disabled .rx-div-edit:focus{
    border-color: #c7c7c7;
}

.rx-div-edit-placeholder{
    position: absolute;
    left: 4px;
    top: 1px;
    line-height: 28px;
    color: #c7c7c7;
}
.rx-div-search-box{
    display: none;
    position: absolute;
    top: 100%;
    width: 100%;
    outline: none;
    border-radius: 4px;
    margin-top: 4px;
    box-shadow: 0 2px 8px rgba(0 ,0 ,0 ,.15);
    max-height: 200px;
    overflow: auto;
}
.rx-div-search-box-show{
    display: block;
    background-color: #fff;
}
.rx-div-search-box-top{
    top: auto;
    bottom: 100%;
    margin-top: 0;
    margin-bottom: 4px;
    max-height: 200px;
    background-color: #fff;
}
.rx-div-edit-disabled {
    background:#f3f3f3;
}

.rx-div-edit-disabled .rx-tags-ul .rx-tags-li{
    padding-right: 4px;
    background-color:#f5f5f5 ;
    color: rgba(0, 0, 0, 0.33);
}
.rx-div-edit-disabled .rx-edit-button{
    background-color: #f5f5f5;
}
.rx-div-edit-disabled .rx-edit-button:hover{
    border-color: #c7c7c7;
    color: rgba(0, 0, 0, 0.65);
    cursor: auto;
}
.rx-div-edit-readonly .rx-tags-ul .rx-tags-li{
    padding-right: 4px;
}
.rx-div-edit-readonly .rx-div-edit-container{
    border-radius: 4px!important;
    border: 1px solid #f8f8f8!important;
    background-color: #f8f8f8;
}
.rx-div-edit-readonly .rx-div-edit-text{
    position: initial;
    white-space: normal;
    overflow: auto;
    text-overflow: initial;
    line-height: 18px;
}
</style>
